﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ProcessFile
{
    public class ConvertStk4AD14
    {
        public static string ConvertFilestk4Ad14(string filePath)
        {

            StkFile file = new StkFile(filePath);
            string content = "";
            string[] lines = file._lines;
            int dieliectricCount = 0;
            int topLayer = file.TopLayer;
            int topSolder = file.TopSolder;
            int bottomLayer = file.BottomLayer;
            int bottomSolder = file.BottomSolder;
            int id = Common.StartIdSeq;
            string Unit = Common.GetUnit(lines);
            ConvertStk4StackupFile convertStk4StackupFile = new ConvertStk4StackupFile();
            convertStk4StackupFile.Id0 = id + 1;
            convertStk4StackupFile.Id1 = id + 2;
            content += convertStk4StackupFile.Hearder;

            string subStackId = convertStk4StackupFile.SubId;
            int indexLayer = 0;

            for (int i = 0; i <= Math.Max(file.BottomSolder, file.BottomLayer); i++)
            {
                string[] item = lines[i].Split('^');
                if (item.Length >= 7)
                {
                    id++;
                    ConvertStk4StackupLayerRow convertStk4Stackup = new ConvertStk4StackupLayerRow(lines[i], indexLayer);
                    if (Common.IsDielectric(item[Common.DescriptionIndex]) && i != topSolder && i != bottomSolder)
                    {
                        dieliectricCount++;
                        convertStk4Stackup.DieliectricIndex = dieliectricCount;
                    }
                   
                    convertStk4Stackup.SubstacksID = new string[]{filePath};
                    convertStk4Stackup.Unit = Unit;
               
                    if (i == topLayer) convertStk4Stackup.IsTopLayer = true;
                    if (i == topSolder) convertStk4Stackup.IsTopSolder = true;
                    if (i == bottomLayer) convertStk4Stackup.IsBottomLayer = true;
                    if (i == bottomSolder) convertStk4Stackup.IsBottomSolder = true;
                    content += convertStk4Stackup.ToString();
                    indexLayer++;
                }

            }
            content += convertStk4StackupFile.Footer(indexLayer + 2, id);
            content = "|" + content.Replace("\r\n", "|");
            return content;
        }
        public static string ConvertFilestk4Ad14(string[] filePath,string fileAd14)
        {
            //file master
            StkFile file = new StkFile(filePath[0]);
            List<string> filesName=new List<string>();
            foreach (string s in filePath)
            {
                FileInfo fileInfo = new FileInfo(s);
                filesName.Add(fileInfo.Name.Replace(fileInfo.Extension,""));
            }
            AD14File ad14File = new AD14File(fileAd14);
            string header = ad14File.Header;
            string footer = ad14File.Footer;
             int maxLevelLayer = Math.Max(file.BottomSolder, file.BottomLayer); ;
             List<string[]> contentFiles = new List<string[]>();
             contentFiles.Add(file._lines);
            for (int i = 1; i < filePath.Length; i++)
            {
                StkFile fileTmp = new StkFile(filePath[i]);
                contentFiles.Add(fileTmp._lines);
                int max = Math.Max(fileTmp.BottomSolder, fileTmp.BottomLayer);
                if (max>maxLevelLayer)
                {
                    file = fileTmp;
                }
            }
            string content = "";
            string[] lines = file._lines;
            int dieliectricCount = 0;
            int topLayer = file.TopLayer;
            int topSolder = file.TopSolder;
            int bottomLayer = file.BottomLayer;
            int bottomSolder = file.BottomSolder;
            int id = Common.StartIdSeq;
            string Unit = Common.GetUnit(lines);
            ConvertStk4StackupFileStage2 convertStk4StackupFile = new ConvertStk4StackupFileStage2();
            convertStk4StackupFile.FilesStk = filesName.ToArray();
            convertStk4StackupFile.Id0 = id + 1;
            convertStk4StackupFile.Id1 = id + 2;
            content += ad14File.Header;

            List<string> subStacksId = convertStk4StackupFile.SubIds;
            
            int indexLayer = 0;
            List<string> layerContent = ad14File.LayerConten();
            for (int i = 0; i <= Math.Max(file.BottomSolder, file.BottomLayer); i++)
            {
                string[] item = lines[i].Split('^');
                if (item.Length >= 7)
                {
                    id++;
                    //ConvertStk4StackupLayerRow convertStk4Stackup = new ConvertStk4StackupLayerRow(lines[i], indexLayer);
                    //if (Common.IsDielectric(item[Common.DescriptionIndex]) && i != topSolder && i != bottomSolder)
                    //{
                    //    dieliectricCount++;
                    //    convertStk4Stackup.DieliectricIndex = dieliectricCount;
                    //}
                    //convertStk4Stackup.contentFileStk = contentFiles;
                    //convertStk4Stackup.SubstacksID = subStacksId;
                    //convertStk4Stackup.Unit = Unit;

                    //if (i == topLayer) convertStk4Stackup.IsTopLayer = true;
                    //if (i == topSolder) convertStk4Stackup.IsTopSolder = true;
                    //if (i == bottomLayer) convertStk4Stackup.IsBottomLayer = true;
                    //if (i == bottomSolder) convertStk4Stackup.IsBottomSolder = true;
                    ConvertStk4StackupLayerRowTemplace convertStk4Stackup = new ConvertStk4StackupLayerRowTemplace(lines[i], layerContent[indexLayer], indexLayer,subStacksId);
                    convertStk4Stackup.Unit = Unit;
                    content += convertStk4Stackup.ToString();
                    indexLayer++;
                }

            }
                 content += ad14File.Footer;
            content = "|" + content.Replace("\r\n", "|");
            return content;
        }
    }
}
